package com.stenio.fancat.service.baby;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stenio.fancat.domain.baby.FcBehavior;
import com.stenio.fancat.domain.baby.FcBehaviorImage;
import com.stenio.fancat.domain.shop.FcVender;
import com.stenio.fancat.domain.shop.FcVenderImage;
import com.stenio.fancat.form.baby.BehaviorForm;
import com.stenio.fancat.form.shop.VenderForm;
import com.stenio.fancat.mapper.baby.FcBehaviorMapper;
import com.stenio.fancat.util.StringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liuan
 * @create 2019-01-16 18:21
 */
@Service
public class BehaviorService extends ServiceImpl<FcBehaviorMapper, FcBehavior> {

    @Autowired
    private FcBehaviorMapper fcBehaviorMapper;

    @Autowired
    private BehaviorImageService behaviorImageService;

    /**
     * 保存任务
     * @param form
     * @return
     */
    public FcBehavior saveTask(BehaviorForm form){
        FcBehavior behavior = new FcBehavior();
        BeanUtils.copyProperties(form,behavior);
        Date date = new Date();
        String behaviorId = StringUtil.getUUID();
        behavior.setCreateTime(date);
        behavior.setUpdateTime(date);
        behavior.setBehaviorId(behaviorId);
        fcBehaviorMapper.insert(behavior);

        //处理关联图片的逻辑
        if(CollectionUtils.isNotEmpty(form.getBehaviorImages())){
            form.getBehaviorImages().stream().forEach(vo->{
                behaviorImageService.updateBiz(behaviorId,vo);
            });
        }
        return behavior;
    }

    /**
     * 根据条件查询
     * @param behaviorForm
     * @return
     */
    public Page<FcBehavior> selectByPage(BehaviorForm behaviorForm){
        FcBehavior fcBehavior = new FcBehavior();
        BeanUtils.copyProperties(behaviorForm,fcBehavior);
        Page<FcBehavior> page = new Page<>(behaviorForm.getPageNo(), behaviorForm.getPageSize());
        page.setRecords(fcBehaviorMapper.selectByConditionPage(page,fcBehavior));
        return page;
    }

    /**
     * 活动上下架
     * @param id
     * @param status
     * @throws Exception
     */
    public FcBehavior onOff(String id, Integer status){
        FcBehavior behavior = this.getById(id);
        behavior.setStatus(status);
        fcBehaviorMapper.updatePart(behavior);
        return behavior;
    }

    /**
     * 根据主键查询
     * @param id
     * @return
     */
    public FcBehavior getById(String id){
        return fcBehaviorMapper.selectById(id);
    }

    /**
     * 更新活动
     * @param form
     */
    public FcBehavior updateVender(BehaviorForm form){
        FcBehavior behavior = new FcBehavior();
        BeanUtils.copyProperties(form,behavior);
        fcBehaviorMapper.updateAll(behavior);
        //处理关联图片的逻辑
        FcBehaviorImage behaviorImage = new FcBehaviorImage();
        behaviorImage.setBehaviorId(form.getBehaviorId());
        List<FcBehaviorImage> relatedImages = new ArrayList<>();
        relatedImages.addAll(behaviorImageService.selectByType(behaviorImage));
        //对于已经建立过关联的记录
        List<Integer> relatedIds = new ArrayList<>();
        relatedImages.stream().forEach(vo->{
            //前端记录中不存在，则删除该存在的记录
            if(!form.getBehaviorImages().contains(vo.getId())){
                behaviorImageService.deleteById(vo.getId());
            }
            relatedIds.add(vo.getId());
        });
        //对于前端传入的记录
        if(CollectionUtils.isNotEmpty(form.getBehaviorImages())){
            form.getBehaviorImages().stream().forEach(vo->{
                //若已关联的记录中，不存在该记录，则建立关联，否则保持不动
                if(!relatedIds.contains(vo)){
                    behaviorImageService.updateBiz(behavior.getBehaviorId(),vo);
                }
            });
        }
        return behavior;
    }

}
